package com.webcash.CNBoard.Controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.webcash.CNBoard.Model.Board;
import com.webcash.CNBoard.Model.CalendarModel;
import com.webcash.CNBoard.Model.Department;
import com.webcash.CNBoard.Model.EmpBoard;
import com.webcash.CNBoard.Model.EmpComment;
import com.webcash.CNBoard.Model.Employee;
import com.webcash.CNBoard.Model.Login;
import com.webcash.CNBoard.Model.PositionName;
import com.webcash.CNBoard.Model.Schedule;
import com.webcash.CNBoard.Model.ScheduleMonthly;
import com.webcash.CNBoard.Model.Work;
import com.webcash.CNBoard.Service.BoardService;
import com.webcash.CNBoard.Service.DepartmentService;
import com.webcash.CNBoard.Service.EmployeeService;
import com.webcash.CNBoard.Service.PositionNameService;
import com.webcash.CNBoard.Service.ProjectService;
import com.webcash.CNBoard.util.CalendarUtil;

@Controller
@RequestMapping("/employee")
@SessionAttributes("login")
public class EmployeeMyPageController {

	@Autowired
	EmployeeService employeeService;

	@Autowired
	DepartmentService departmentService;
	
	@Autowired
	BoardService boardService;

	@Autowired
	ProjectService projectService;
	
	@Autowired
	PositionNameService positionNameService;
	
	
	@RequestMapping
	public String action(@ModelAttribute Login login, Model model) {

		Employee emp = employeeService.getEmployeeById(login.getId());

		Department dept = departmentService.getDepartmentByDeptNo(Integer
				.parseInt(emp.getDeptNo()));
		
		int pageNum = 1;
		List<EmpBoard> boardList = boardService.getEmpBoardListByEmpNo(emp
				.getEmpNo(), pageNum);
		
		List<Work> workList = projectService.getNewAllWorksById(login.getId());
		int count = boardService.getEmpBoardCountByEmpNo(emp.getEmpNo());
		PositionName emppos = positionNameService.getPositionNamebyPositionNo(Integer.parseInt(emp.getPosition()));
		
		model.addAttribute("emp", emp);
		model.addAttribute("dept", dept);
		model.addAttribute("boardList", boardList);
		model.addAttribute("write", false);
		model.addAttribute("workList", workList);
		model.addAttribute("emppos", emppos);
		model.addAttribute("boardCount", count);
		model.addAttribute("pageNum", pageNum);
		
		return "mypage/myPages";
	}

	@RequestMapping("/write") // 글쓰기 페이지 이동
	public String write(Model model) {

		return "mypage/boardWrite";
	}
	
	@RequestMapping("/modify")
	public String modify(@ModelAttribute Login login, @RequestParam int empBoardNo,  Model model){
		Employee emp = employeeService.getEmployeeById(login.getId());
		EmpBoard empBoard = boardService.getEmpBoardByEmpNoAndEmpBoardNo(emp.getEmpNo(), empBoardNo);
		System.out.println(empBoard);
		model.addAttribute("empBoard", empBoard);
		return "mypage/boardModify";
	}

	@RequestMapping(value = "/writeAction" , method=RequestMethod.POST ) //글쓰기 ajax
	@ResponseBody
	public String writeAction(@ModelAttribute Login login, @ModelAttribute Board board,  Model model) {
		
		System.out.println(board);
		String data = "fail"; //or success 로 구분해서 ajax 컨트롤 유효성 검사하고 title이나 article 비어있는지
		
		Employee emp = employeeService.getEmployeeById(login.getId());
		boardService.addEmpBoardByEmpNo(login.getId(), board, emp.getEmpNo());
		
		return data;
	}
	
	@RequestMapping(value = "/modifyAction" , method=RequestMethod.POST ) //글쓰기 ajax
	@ResponseBody
	public String modifyAction(@ModelAttribute Login login, @ModelAttribute Board board, @RequestParam int empBoardNo, Model model) {
		
		Employee emp = employeeService.getEmployeeById(login.getId());
		boardService.updateEmpBoardByBoardNo(board, empBoardNo, emp.getEmpNo());
		return "";
	}
	@RequestMapping(value = "/list") // 게시물 리스트
	public String list(@ModelAttribute Login login, @RequestParam(required=false) String num, Model model) {
		
		Employee emp = employeeService.getEmployeeById(login.getId());
		
		Department dept = departmentService.getDepartmentByDeptNo(Integer
				.parseInt(emp.getDeptNo()));
		int count = boardService.getEmpBoardCountByEmpNo(emp.getEmpNo());
		int pageNum = 1;
		if(num == null)
			System.out.println("num is null");
		else{
			System.out.println(num);
			pageNum = Integer.parseInt(num);
			
		}
		
		List<EmpBoard> boardList = boardService.getEmpBoardListByEmpNo(emp
				.getEmpNo(), pageNum);
		
		model.addAttribute("emp", emp);
		model.addAttribute("dept", dept);
		model.addAttribute("boardList", boardList);
		model.addAttribute("boardCount", count);
		model.addAttribute("pageNum", pageNum);
		
		return "mypage/board";
	}

	@RequestMapping(value="/contents/{empBoardNo}") //게시물 선택
	public String listAction(@PathVariable int empBoardNo, @RequestParam int num, @RequestParam(required=false) boolean updateComment, @ModelAttribute Login login, Model model){
		
		Employee emp = employeeService.getEmployeeById(login.getId());
		System.out.println(updateComment);
		if(!updateComment)
			boardService.updateEmpBoardForReadCountByEmpNoAndEmpBoardNo(emp.getEmpNo(), empBoardNo);
		EmpBoard empBoard = boardService.getEmpBoardByEmpNoAndEmpBoardNo(emp.getEmpNo(), empBoardNo);
		List<EmpComment> commentList = boardService.getEmpCommentListByBoardNo(empBoard.getBoardNo());
		
		model.addAttribute("empBoard", empBoard);
		model.addAttribute("pageNum", num);
		model.addAttribute("commentList", commentList);
		return "mypage/boardContents";
	}
	
	@RequestMapping(value="/delete/{empBoardNo}") //게시판 삭제
	public String deleteAction(@PathVariable int empBoardNo, @ModelAttribute Login login, Model model){
		Employee emp = employeeService.getEmployeeById(login.getId());
		boardService.removeEmpBoardByEmpNoAndEmpBoardNo(emp.getEmpNo(), empBoardNo);
		
		return "mypage/board";
	}
	
	@RequestMapping(value="/progress") //업무 progress 비동기
	public String getProgressWorks(@ModelAttribute Login login, Model model){
		
		List<Work> workList = projectService.getProgressWorkListById(login.getId());
		
		model.addAttribute("workList", workList);
		return "mypage/myWorksContent";
	}
	@RequestMapping(value="/order") //업무 order 비동기 
	public String getOrderWorks(@ModelAttribute Login login, Model model){
		
		List<Work> workList = projectService.getNewAllWorksById(login.getId());
		
		model.addAttribute("workList", workList);
		return "mypage/myWorksContent";
	}
	
	@RequestMapping(value="/schedule")
	public String getSchedule(@ModelAttribute Login login, Model model){
		
		Employee emp = employeeService.getEmployeeById(login.getId());
		
		CalendarUtil calUtil = new CalendarUtil();
		
		CalendarModel cal = calUtil.calendar();
		List<Work> workList = projectService.getAllWorkListById(login.getId());
		
		List<Schedule> scList = employeeService.getScheduleListByEmpNo(emp.getEmpNo());
		List<ScheduleMonthly> monthlyList = employeeService.getScheduleMonthlyListByEmpNo(emp.getEmpNo());
		
		model.addAttribute("calendar", cal);
		model.addAttribute("workList", workList);
		model.addAttribute("emp", emp);
		model.addAttribute("scList", scList);
		model.addAttribute("monthlyList", monthlyList);
		
		return "mypage/schedule";
	}
	@RequestMapping(value="/schedule/add")
	public String addSchedule(@ModelAttribute Login login, @RequestParam int empNo, Model model){
		Employee emp = employeeService.getEmployeeById(login.getId());
		
		model.addAttribute("emp", emp);
		return "mypage/scheduleAdd";
	}
	@RequestMapping(value="/schedule/add/action")
	public String addActionSchedule(@ModelAttribute Login login, @RequestParam int empNo, @ModelAttribute Schedule schedule, Model model){
		
		
		employeeService.addScheduleByEmpNoAndSchedule(empNo, schedule);

		Employee emp = employeeService.getEmployeeById(login.getId());
		model.addAttribute("emp", emp);
		return "mypage/scheduleAdd";
	}
	@RequestMapping(value="/schedule/add/action/weekly")
	public String addActionScheduleWeekly(@ModelAttribute Login login, @ModelAttribute ScheduleMonthly scheduleMonthly, @RequestParam int empNo,  Model model){
		
		System.out.println("empNo" + empNo);
		
		employeeService.addScheduleMonthlyByEmpNoAndScheduleMonthly(empNo, scheduleMonthly);
		
		Employee emp = employeeService.getEmployeeById(login.getId());
		model.addAttribute("emp", emp);
		return "mypage/scheduleAdd";
	}
	@RequestMapping(value="/schedule/add/action/monthly")
	public String addActionScheduleMonthly(@ModelAttribute Login login, @ModelAttribute ScheduleMonthly schedule, @RequestParam boolean pushWeekend, @RequestParam int empNo,  Model model){
		
		ScheduleMonthly scheduleMonthly = schedule;
		System.out.println(pushWeekend);
		scheduleMonthly.setExceptWeekend(pushWeekend);
		
		employeeService.addScheduleMonthlyByEmpNoAndScheduleMonthly(empNo, scheduleMonthly);
		
		Employee emp = employeeService.getEmployeeById(login.getId());
		model.addAttribute("emp", emp);
		return "mypage/scheduleAdd";
	}
	@RequestMapping(value="/schedule/list")
	public String listSchedule(@ModelAttribute Login login, @RequestParam int empNo, Model model){
		Employee emp = employeeService.getEmployeeById(login.getId());
		List<Schedule> scheduleList = employeeService.getScheduleListByEmpNo(empNo);
		List<ScheduleMonthly> monthlyList = employeeService.getScheduleMonthlyListByEmpNo(emp.getEmpNo());
		model.addAttribute("monthlyList", monthlyList);
		model.addAttribute("emp", emp);
		model.addAttribute("scList", scheduleList);
		return "mypage/scheduleList";
	}
	@RequestMapping(value="/schedule/delete")
	@ResponseBody
	public String deleteSchedule(@ModelAttribute Login login, @RequestParam int scheduleNo, Model model){
		employeeService.removeScheduleByScheduleNo(scheduleNo);
		return "success";
	}
	
	@RequestMapping(value="/schedule/changeMonth")
	public String changeSchedule(@ModelAttribute Login login,@RequestParam int year, @RequestParam int month, Model model){
		
		Employee emp = employeeService.getEmployeeById(login.getId());
		
		CalendarUtil calUtil = new CalendarUtil(year, month);
		
		CalendarModel cal = calUtil.calendar();
		List<Work> workList = projectService.getAllWorkListById(login.getId());
		List<Schedule> scList = employeeService.getScheduleListByEmpNo(emp.getEmpNo());
		List<ScheduleMonthly> monthlyList = employeeService.getScheduleMonthlyListByEmpNo(emp.getEmpNo());
		
		model.addAttribute("monthlyList", monthlyList);
		model.addAttribute("calendar", cal);
		model.addAttribute("workList", workList);
		model.addAttribute("emp", emp);
		model.addAttribute("scList", scList);
		return "mypage/schedule";
	}
	
	@RequestMapping(value="/schedule/week")
	public String scheduleWeek(@ModelAttribute Login login, @RequestParam int year, @RequestParam int month, @RequestParam int day, Model model){
		System.out.println(year + "/" + month + "/" + day + "/hhhh");
		Employee emp = employeeService.getEmployeeById(login.getId());
		String d = new String();
		CalendarUtil calUtil = new CalendarUtil();
		
		CalendarModel cal = calUtil.calendar();
		cal = calUtil.week(cal, year, month, day);
		
		List<Work> workList = projectService.getAllWorkListById(login.getId());
		List<Schedule> scList = employeeService.getScheduleListByEmpNo(emp.getEmpNo());
		List<ScheduleMonthly> monthlyList = employeeService.getScheduleMonthlyListByEmpNo(emp.getEmpNo());
		
		System.out.println(monthlyList);
		model.addAttribute("monthlyList", monthlyList);
		model.addAttribute("calendar", cal);
		model.addAttribute("workList", workList);
		model.addAttribute("emp", emp);
		model.addAttribute("scList", scList);
		
		
		return "mypage/scheduleWeek";
	}
	
	@RequestMapping(value="/schedule/week/change")
	public String changeWeek(@ModelAttribute Login login, @RequestParam int year, @RequestParam int month, @RequestParam int day,  @RequestParam boolean upAndDown, Model model){
		System.out.println(upAndDown);
		Employee emp = employeeService.getEmployeeById(login.getId());
		
		CalendarUtil calUtil = new CalendarUtil();
		
		CalendarModel cal = calUtil.calendar();
		System.out.println(year +"/" + month +"/" + day);
		cal = calUtil.weekUpAndDown(cal, year, month, day, upAndDown);
		
		List<Work> workList = projectService.getAllWorkListById(login.getId());
		List<Schedule> scList = employeeService.getScheduleListByEmpNo(emp.getEmpNo());
		List<ScheduleMonthly> monthlyList = employeeService.getScheduleMonthlyListByEmpNo(emp.getEmpNo());
		
		model.addAttribute("monthlyList", monthlyList);
		model.addAttribute("calendar", cal);
		model.addAttribute("workList", workList);
		model.addAttribute("emp", emp);
		model.addAttribute("scList", scList);
		
		
		return "mypage/scheduleWeek";
	}
	
	@RequestMapping(value="/comment/write")
	@ResponseBody
	public String commentWrite(@ModelAttribute Login login,@RequestParam int boardNo, @RequestParam String commentContents,  Model model){
		System.out.println(commentContents +"/" + boardNo);
		
		boardService.addEmpCommentByBoardNoAndId(boardNo, login.getId(), commentContents);
		
		return "";
	}
	

}
